1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22 import com.google.common.base.Function;
23 import com.google.common.collect.ForwardingQueue;
24 import com.google.common.collect.MinMaxPriorityQueue;
25 import com.google.common.collect.Ordering;
26
27 import java.math.BigInteger;
28 import java.util.Comparator;
29 import java.util.PriorityQueue;
30 import java.util.Queue;
31 import java.util.Random;
32
33
34
35
36
37
38 public class MinMaxPriorityQueueBenchmark {
39 @Param private ComparatorType comparator;
40
41
42
43 @Param({"100", "10000"}) private int size;
44
45 @Param private HeapType heap;
46
47 private Queue<Integer> queue;
48
49 private final Random random = new Random();
50
51 @BeforeExperiment void setUp() {
52 queue = heap.create(comparator.get());
53 for (int i = 0; i < size; i++) {
54 queue.add(random.nextInt());
55 }
56 }
57
58 @Benchmark void pollAndAdd(int reps) {
59 for (int i = 0; i < reps; i++) {
60
61 queue.add(queue.poll() ^ random.nextInt());
62 }
63 }
64
65 @Benchmark void populate(int reps) {
66 for (int i = 0; i < reps; i++) {
67 queue.clear();
68 for (int j = 0; j < size; j++) {
69
70 queue.add(random.nextInt());
71 }
72 }
73 }
74
75
76
77
78
79
80 static final class InvertedMinMaxPriorityQueue <T> extends ForwardingQueue<T> {
81 MinMaxPriorityQueue<T> mmHeap;
82 public InvertedMinMaxPriorityQueue(Comparator<T> comparator) {
83 mmHeap = MinMaxPriorityQueue.orderedBy(comparator).create();
84 }
85
86 @Override
87 protected Queue<T> delegate() {
88 return mmHeap;
89 }
90
91 @Override
92 public T poll() {
93 return mmHeap.pollLast();
94 }
95
96 }
97
98 public enum HeapType {
99 MIN_MAX {
100 @Override public Queue<Integer> create(Comparator<Integer> comparator) {
101 return MinMaxPriorityQueue.orderedBy(comparator).create();
102 }
103 },
104 PRIORITY_QUEUE {
105 @Override public Queue<Integer> create(Comparator<Integer> comparator) {
106 return new PriorityQueue<Integer>(11, comparator);
107 }
108 },
109 INVERTED_MIN_MAX {
110 @Override public Queue<Integer> create(Comparator<Integer> comparator) {
111 return new InvertedMinMaxPriorityQueue<Integer>(comparator);
112 }
113 };
114
115 public abstract Queue<Integer> create(Comparator<Integer> comparator);
116 }
117
118
119
120
121
122 static class ExpensiveComputation implements Function<Integer, BigInteger> {
123 @Override
124 public BigInteger apply(Integer from) {
125 BigInteger v = BigInteger.valueOf(from);
126
127
128 for (double i = 0; i < 100; i += 20) {
129 v = v.add(v.multiply(
130 BigInteger.valueOf(
131 ((Double) Math.abs(Math.sin(i) * 10.0)).longValue())));
132 }
133 return v;
134 }
135 }
136
137 public enum ComparatorType {
138 CHEAP {
139 @Override public Comparator<Integer> get() {
140 return Ordering.natural();
141 }
142 },
143 EXPENSIVE {
144 @Override public Comparator<Integer> get() {
145 return Ordering.natural().onResultOf(new ExpensiveComputation());
146 }
147 };
148 public abstract Comparator<Integer> get();
149 }
150 }